package stanaGUI;
import java.util.List;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PiePlot3D;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.general.PieDataset;
import org.jfree.data.statistics.HistogramDataset;
import org.jfree.data.statistics.HistogramType;
import org.jfree.data.xy.IntervalXYDataset;
import org.jfree.util.Rotation;

import stanaGUI.Hierarchical.Cluster;

import stanaGUI.Hierarchical.*;
import stanaGUI.Hierarchical.*;
public class GraphicalRepresentationHierarchical {
	
	public ChartPanel PieChart (
			String applicationTitle, 
			String chartTitle,
			List<Cluster> clusters2
			) {
		DefaultPieDataset result = new DefaultPieDataset();
        // This will create the dataset 
        PieDataset dataset = createPieDataset(result,clusters2);
        // based on the dataset we create the chart
        JFreeChart chart = createPieChart(dataset, chartTitle);
        // we put the chart into a panel
        ChartPanel chartPanel = new ChartPanel(chart);
        
        // default size
        chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
        // add it to our application
         return chartPanel;

    }
	
   public ChartPanel Histogram(String title, List<stanaGUI.Hierarchical.Cluster> list) {
        IntervalXYDataset dataset = createHistogramDataset(list);
        JFreeChart chart = createHistogramChart(dataset);
        ChartPanel chartPanel = new ChartPanel(chart);
        chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
        chartPanel.setMouseZoomable(true, false);
        return chartPanel;
   }
	 	
 
    /**
     * Creates a chart
     */
    private JFreeChart createPieChart(PieDataset dataset, String title) {
        
        JFreeChart chart = ChartFactory.createPieChart3D(title,  				// chart title
            dataset,                // data
            true,                   // include legend
            true,
            false);

        PiePlot3D plot = (PiePlot3D) chart.getPlot();
        plot.setStartAngle(290);
        plot.setDirection(Rotation.CLOCKWISE);
        plot.setForegroundAlpha(0.5f);
        return chart;
        
    }
	
    /**
     * Creates a Histogram chart.
     * 
     * @param dataset  a dataset.
     * 
     * @return The chart.
     */
    private JFreeChart createHistogramChart(IntervalXYDataset dataset) {
        JFreeChart chart = ChartFactory.createHistogram(
            "Histogram", 
            null, 
            null, 
            dataset, 
            PlotOrientation.VERTICAL, 
            true, 
            false, 
            false
        );
        chart.getXYPlot().setForegroundAlpha(0.75f);
        return chart;
    }
    
    
    
    /**
     * Creates a sample dataset 
     */
    private  PieDataset createPieDataset(DefaultPieDataset result, List<stanaGUI.Hierarchical.Cluster> clusters2) {
    	// Now we need to add the dataset values
    	for (stanaGUI.Hierarchical.Cluster cluster1 : clusters2) {
			for (stanaGUI.Hierarchical.Item item : cluster1.getItems()) {
				result.setValue(Double.toString(item.getValue1()), item.getValue2());
			}

    	}
    	return result;
        
    }
    /**
     * Creates a sample {@link HistogramDataset}.
     * 
     * @return The dataset.
     */
    private IntervalXYDataset createHistogramDataset(List<stanaGUI.Hierarchical.Cluster> clusters) {
        HistogramDataset dataset = new HistogramDataset();
        dataset.setType(HistogramType.RELATIVE_FREQUENCY);
        int i = 0;
        int numOfItems = clusters.size();
        double[] quantity = new double[numOfItems];
        double[] prices = new double[numOfItems];
       	for (stanaGUI.Hierarchical.Cluster cluster1 : clusters) {
    			for (stanaGUI.Hierarchical.Item item : cluster1.getItems()) {
    				quantity[i] = item.getValue1();
    				prices[i] = item.getValue2();
    				i++;
    			}
       	}
        dataset.addSeries("Value1 ", quantity, 10);
        dataset.addSeries("Value2 ", prices, 12);   
        return dataset;     
    }
    

}